package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 19shusheng.com
 * @Description:
 * @Date: Created in 22:07 2020/10/19
 * @Modified By:
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    /**
     *
     * @Description: 浏览器不支持post请求
     *           测试需要用postman
     *
     * @auther: madonghui
     * @date: 22:40 2020/10/19
     * @param:
     * @return:
     *
     */
    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping("/create")
    public CommonResult<Object> create(@RequestBody Payment payment){
        int i = paymentService.create(payment);
        if (i > 0){
            return new CommonResult<>(200,"插入成功:serverPort"+serverPort,i);
        }else {
            return new CommonResult<>(500,"插入失败");
        }
    }

    @GetMapping("getPaymentById/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id")Long id){
        Payment paymentById = paymentService.getPaymentById(id);
        if (Objects.nonNull(paymentById)){
            return new CommonResult<>(200,"查询成功w(ﾟДﾟ)w:serverPort"+serverPort,paymentById);
        }else {
            return new CommonResult<>(500,"查询失败，没有对应的数据");
        }
    }

    @GetMapping("/getDiscoveryInfo")
    public CommonResult getDiscoveryInfo(){
        /**
         *
         * @Description:
         *    获取到注册进eureka中的所有的服务名称
         * @auther: madonghui
         * @date: 18:29 2020/10/25
         * @param: []
         * @return: com.atguigu.springcloud.entities.CommonResult
         *
         */
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            System.out.println("service = " + service);
        }
        /**
         *
         * @Description:
         *   获取注册到eureka中的指定名称的微服务实例信息
         * @auther: madonghui
         * @date: 18:32 2020/10/25
         * @param: []
         * @return: com.atguigu.springcloud.entities.CommonResult
         *
         */
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            /**
             *
             * @Description:
             * 获取实例名称
             * @auther: madonghui
             * @date: 19:02 2020/10/25
             * @param: []
             * @return: com.atguigu.springcloud.entities.CommonResult
             *
             */
            String instanceId = instance.getInstanceId();
            System.out.println("instanceId = " + instanceId);
            /**
             *
             * @Description:
             * 获取实例的主机
             * @auther: madonghui
             * @date: 19:02 2020/10/25
             * @param: []
             * @return: com.atguigu.springcloud.entities.CommonResult
             *
             */
            String host = instance.getHost();
            System.out.println("host = " + host);
            /**
             *
             * @Description:
             * 获取当前实例的端口号
             * @auther: madonghui
             * @date: 19:06 2020/10/25
             * @param: []
             * @return: com.atguigu.springcloud.entities.CommonResult
             *
             */
            int port = instance.getPort();
            System.out.println("port = " + port);
            /**
             *
             * @Description: 获取当前实例的元数据
             *
             * @auther: madonghui
             * @date: 19:07 2020/10/25
             * @param: []
             * @return: com.atguigu.springcloud.entities.CommonResult
             *
             */
            Map<String, String> metadata = instance.getMetadata();
            System.out.println("metadata = " + metadata);
            /**
             *
             * @Description: 获取当前服务的uri访问地址
             *
             * @auther: madonghui
             * @date: 19:09 2020/10/25
             * @param: []
             * @return: com.atguigu.springcloud.entities.CommonResult
             *
             */
            URI uri = instance.getUri();
            System.out.println("uri = " + uri);
            /**
             *
             * @Description:
             *
             * @auther: madonghui
             * @date: 19:10 2020/10/25
             * @param: []
             * @return: com.atguigu.springcloud.entities.CommonResult
             *
             */
            String scheme = instance.getScheme();
            System.out.println("scheme = " + scheme);
            /**
             *
             * @Description: 判断当前实例是否安全
             *
             * @auther: madonghui
             * @date: 19:10 2020/10/25
             * @param: []
             * @return: com.atguigu.springcloud.entities.CommonResult
             *
             */
            boolean secure = instance.isSecure();
            System.out.println("secure = " + secure);
        }
        return new CommonResult(200,"查询成功",services+"/t"+instances);
    }

    @GetMapping("/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @GetMapping("/paymentTimeout")
    public String paymentTimeout(){
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (Exception e){
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping("zipkin")
    public String paymentZipkin()
    {
        return "hi ,i'am paymentzipkin server fall back，welcome to atguigu，O(∩_∩)O哈哈~";
    }
}
